home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1997 #1 / Amiga Plus Extra 1997 #1.iso / programme / tools / leoutils / memtest.c < prev    next >
C/C++ Source or Header  |  1994-06-15  |  4KB  |  167 lines

  1. #include <stdlib.h>
  2. #include <exec/memory.h>
  3. #if defined __SASC
  4. #include <proto/exec.h>
  5. #include <proto/dos.h>
  6. #else
  7. #include <clib/exec_protos.h>
  8. #include <clib/dos_protos.h>
  9. #endif
  10.  
  11. static char *Ver =
  12.         "$VER:MemTest 1.1"
  13. #if defined __SASC
  14.         " "    __AMIGADATE__ " ©1994 Leopold-Soft, leopold@cs.tut.fi"
  15. #endif
  16.         ;
  17.  
  18. struct MemNode {
  19.     struct MemNode *NextNode;
  20.     long Bytes;
  21. };
  22.  
  23. struct MemTypeStruct {
  24.     char *Text;
  25.     unsigned long Value;
  26. };
  27.  
  28. static struct MemTypeStruct MemType[] = {
  29.     {"24BITDMA", 1L<<9},
  30.     {"CHIP", 1L<<1},
  31.     {"FAST", 1L<<2},
  32.     {"PUBLIC", 1L<<0},
  33.     {"LOCAL", 1L<<8},
  34.     {NULL, 0}
  35. };
  36.  
  37. int main(void) {
  38.     static struct MemNode *FirstNode = NULL, *CurrNode = NULL, *LastNode, *NextNode;
  39.     static short OK, tmp;
  40.     static long BufSize = 64*1024, i, Errors = 0, Bytes = 0, OriginalPri, MyType;
  41.     volatile unsigned long *CheckLong;    /* If these weren't "volatile", the compiler    */
  42.     volatile unsigned char *CheckByte;    /*    would optimize all tests away!                */
  43.     static struct Process *MyProcess;
  44.     static BPTR OrigOutput, NewOutput = NULL;
  45.     static unsigned char Test00, TestFF;
  46.     static unsigned long Test00000000, TestFFFFFFFF;
  47.  
  48.     MyProcess = (struct Process *) FindTask(0);
  49.     OriginalPri = SetTaskPri((struct Task *) MyProcess, 5);
  50.  
  51.     OrigOutput = Output();
  52.     if (!OrigOutput) {
  53.         NewOutput = Open("CON:10/10/600/300/MemTest ©1994 Leopold-Soft, leopold@cs.tut.fi",
  54.                         MODE_NEWFILE);
  55.         if (NewOutput) {
  56.             SelectOutput(NewOutput);
  57.         }
  58.     }
  59.  
  60.     Printf("\nMemory test v1.1 ©1994 Leopold-Soft, leopold@cs.tut.fi\n"
  61.             "Henrik Herranen, Atomikatu 6A9, SF-33720 Tampere, Finland\n");
  62.  
  63.     do {
  64.         do {
  65.             if (!FirstNode) {
  66.                 FirstNode = AllocMem(BufSize, MEMF_ANY);
  67.                 if (FirstNode) {
  68.                     Bytes += BufSize;
  69.                     CurrNode = FirstNode;
  70.                     CurrNode->NextNode = NULL;
  71.                     CurrNode->Bytes = BufSize;
  72.                     OK = 1;
  73.                 } else {
  74.                     BufSize /= 2;
  75.                     OK = 0;
  76.                 }
  77.             } else {
  78.                 NextNode = AllocMem(BufSize, MEMF_ANY);
  79.                 if (NextNode) {
  80.                     Bytes += BufSize;
  81.                     CurrNode->NextNode = NextNode;
  82.                     CurrNode = NextNode;
  83.                     CurrNode->NextNode = NULL;
  84.                     CurrNode->Bytes = BufSize;
  85.                     OK = 1;
  86.                 } else {
  87.                     BufSize /= 2;
  88.                     OK = 0;
  89.                 }
  90.             }
  91.         } while(!OK && BufSize >= sizeof(struct MemNode));
  92.  
  93.         if (OK) {
  94.             MyType = TypeOfMem(CurrNode);
  95.             Printf("Allocated ");
  96.             if (BufSize >= 1024) Printf("%3ld KB", BufSize/1024);
  97.             else Printf("%4ld B", BufSize);
  98.             Printf(" at $%8lX, type ", CurrNode);
  99.  
  100.             i = 0;
  101.             tmp = 0;
  102.             do {
  103.                 if (MyType & MemType[i].Value) {
  104.                     if (tmp) Printf(" + ");
  105.                     tmp = 1;
  106.                     Printf("%s", MemType[i].Text);
  107.                 }
  108.             } while (MemType[++i].Text);
  109.             if (!tmp) Printf("UNKNOWN!");
  110.             Printf("\n");
  111.  
  112.             CheckLong = (unsigned long *) ((unsigned char *) CurrNode + sizeof(struct MemNode));
  113.  
  114.             i = (BufSize - sizeof(struct MemNode)) / sizeof(unsigned long);
  115.             while (i--) {
  116.                 *CheckLong = 0x00000000;
  117.                 Test00000000 = *CheckLong;
  118.                 *CheckLong = 0xFFFFFFFF;
  119.                 TestFFFFFFFF = *CheckLong;
  120.                 if (Test00000000 || TestFFFFFFFF!=0xFFFFFFFF) {
  121.                     long j = sizeof(unsigned long);
  122.                     CheckByte = (unsigned char *) CheckLong;
  123.                     while (j--) {
  124.                         *CheckByte = 0x00;
  125.                         Test00 = *CheckByte;
  126.                         *CheckByte = 0xFF;
  127.                         TestFF = *CheckByte;
  128.                         if (Test00 || TestFF!=0xFF) {
  129.                             Printf("ERROR: @$%lX, $00->$%02lX, $ff->$%02lX\n",
  130.                                 CheckByte, (unsigned long) Test00, (unsigned long) TestFF);
  131.                             Errors++;
  132.                         }
  133.                         CheckByte++;
  134.                     }
  135.                 }
  136.                 CheckLong++;
  137.             }
  138.         }
  139.     } while(OK);
  140.  
  141.     Printf("\nCheck completed, ");
  142.     if (!Errors) {
  143.         Printf("no errors found in %ld KB (lucky you)!\n\n", Bytes/1024);
  144.     } else {
  145.         Printf("%ld errors found in %ld KB (too bad)!\n\n", Errors, Bytes/1024);
  146.     }
  147.  
  148.     Printf("Freeing resources...\n");
  149.     CurrNode = FirstNode;
  150.     while (CurrNode) {
  151.         LastNode = CurrNode;
  152.         CurrNode = CurrNode->NextNode;
  153.         FreeMem(LastNode, LastNode->Bytes);
  154.     }
  155.     Printf("  ...done!\n\n");
  156.  
  157.     SetTaskPri((struct Task *) MyProcess, OriginalPri);
  158.  
  159.     if (NewOutput) {
  160.         Delay(200);
  161.         SelectOutput(NULL);
  162.         Close(NewOutput);
  163.     }
  164.  
  165.     return (Errors) ? 30 : 0;
  166. }
  167.